package com.highstockings.Interceptor;

import com.highstockings.service.ArticleService;
import com.highstockings.service.GalleryTypeService;
import com.highstockings.service.ModelService;
import com.highstockings.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 前台内容装载
 * Created by diablo13064 on 2018/3/29.
 */
@Component
public class ContentInterceptor implements HandlerInterceptor {
    BeanFactory factory = null;
    @Autowired
    ArticleService articleService;
    @Autowired
    ModelService modelService;
    @Autowired
    GalleryTypeService galleryTypeService;
    @Autowired
    UserService userService;
    private Logger logger = LoggerFactory.getLogger(ContentInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        if ("undefined".equals(httpServletRequest.getRequestURI())) {
            return true;
        }
        factory = WebApplicationContextUtils.getRequiredWebApplicationContext(httpServletRequest.getServletContext());
        if (articleService == null) {
            articleService = (ArticleService) factory.getBean("ArticleService");
        }
        if (modelService == null) {
            modelService = (ModelService) factory.getBean("ModelService");
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        String access_dir = httpServletRequest.getRequestURI();
        //1.左侧边栏模特信息（模特图片，模特名alias,模特评分。模特其他）
        if (access_dir.contains("/change")) {
            return;
        }
        if (access_dir.contains("/commit") || access_dir.contains("/delete")
                || access_dir.contains("/upload") || access_dir.contains("/addModel")
                /*|| access_dir.contains("/gallerytype")*/
                ) {
            return;
        }
        //2.套图信息（套图封面 套图名 ，其他）s
        modelAndView.addObject("picList", articleService.selectFivePics());                     //轮播图
        modelAndView.addObject("tagSet", articleService.findAllTagName());                      //标签信息
        //4、套图分类信息（套图名，查询）
        if (access_dir.contains("/home") || access_dir.contains("/view") || access_dir.contains("/model_details")) {
            modelAndView.addObject("sidebarList1", articleService.findSideBarInfo1());             //侧边栏信息1
            modelAndView.addObject("sidebarList2", modelService.findRecentModelVo());           //侧边栏信息2
        }
        if (access_dir.contains("/modelList")) {
            modelAndView.addObject("sidebarList2", modelService.findSidebarModelInfo());           //侧边栏信息2
        }

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

        //日志处理
        //UserAgent userAgent = UserAgent.parseUserAgentString(httpServletRequest.getHeader("User-Agent"));
        //userAgent.
        /*synchronized (){

        }*/
    }
}
